package org.crumbleworks.crumbchat.server;

import java.io.IOException;

import org.crumbleworks.crumbchat.common.Network;

import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Server;

public class ChatServer extends Server {
    private int tcpPort, udpPort;

    private final int DEFAULT_TCPPORT = 44444;
    private final int DEFAULT_UDPPORT = 44444;

    public ChatServer() {
        this.tcpPort = DEFAULT_TCPPORT;
        this.udpPort = DEFAULT_UDPPORT;

        init();
    }

    public ChatServer(int tcpPort) {
        this.tcpPort = tcpPort;
        this.udpPort = DEFAULT_UDPPORT;

        init();
    }

    public ChatServer(int tcpPort, int udpPort) {
        this.tcpPort = tcpPort;
        this.udpPort = udpPort;

        init();
    }

    private void init() {
        start();
        Network.register(this);
    }

    public void bind() throws IOException {
        bind(tcpPort, udpPort);
    }

    public void unbind() {
        close();
    }

    public int getTcpPort() {
        return tcpPort;
    }

    public void setTcpPort(int tcpPort) {
        this.tcpPort = tcpPort;
    }

    public int getUdpPort() {
        return udpPort;
    }

    public void setUdpPort(int udpPort) {
        this.udpPort = udpPort;
    }

    @Override
    protected Connection newConnection() {
        return new ChatConnection();
    }
}
